/****************************************************************************
**                                                                         **
**                            BPF User Module                              **
**            Copyright (c) 1997 - 2006 microOLAP Technologies LTD,        **
**                       Khalturin A.P., Naumov D.A.                       **
**                               Header File                               **
**                                                                         **
****************************************************************************/

//---------------------------------------------------------------------------
#ifndef __HN_BPF_H__
#define __HN_BPF_H__
//---------------------------------------------------------------------------

#pragma pack(push)
#pragma pack(1)
//---------------------------------------------------------------------------
// HN_BPF_PROGRAMM structure is intended for BPF program creation and is used in the HNUserFilter component. 
//---------------------------------------------------------------------------
typedef struct _HN_BPF_INSTRUCTION
{
    WORD  Code; // Instruction code (see BPF Instructions).
    BYTE  jt;   // Jump if True.
    BYTE  jf;   // Jump if False.
    DWORD k;    // A generic BPF multiuse field.
}HN_BPF_INSTRUCTION, *PHN_BPF_INSTRUCTION;

//---------------------------------------------------------------------------
// HN_BPF_INSTRUCTION is intended for a description of a single BPF instruction, and is used in the HN_BPF_PROGRAMM structure.
//---------------------------------------------------------------------------
typedef struct _HN_BPF_PROGRAMM 
{
    DWORD              CommandCount;    // The total number of instructions in the BPF program.
    HN_BPF_INSTRUCTION Instructions[1]; // Instructions array.
}HN_BPF_PROGRAMM, *PHN_BPF_PROGRAMM;

#pragma pack(pop)

//---------------------------------------------------------------------------
// The instruction encondings.
//---------------------------------------------------------------------------
// instruction classes 
#define BPF_CLASS(code) ((code) & 0x07)
#define         BPF_LD          0x00 // Instruction class. Load a value into the accumulator.
#define         BPF_LDX         0x01 // Instruction class. Load a value into the index register.
#define         BPF_ST          0x02 // Instruction class. Stores the accumulator into the scratch memory.
#define         BPF_STX         0x03 // Instruction class. Stores the index register into the scratch memory.
#define         BPF_ALU         0x04 // Instruction class. Operations between the accumulator and index register or constant, and store the result back in the accumulator.
#define         BPF_JMP         0x05 // Instruction class. The jump instructions alter flow of control.
#define         BPF_RET         0x06 // Instruction class. The return instructions terminate the filter program. A return value of zero indicates that the packet should be ignored.
#define         BPF_MISC        0x07 // Instruction class. All other instructions.

//---------------------------------------------------------------------------
// ld/ldx fields 
#define BPF_SIZE(code)  ((code) & 0x18)
#define         BPF_W           0x00 // Ld/Ldx fields. Data size - word - 32 bits
#define         BPF_H           0x08 // Ld/Ldx fields. Data size - halfword - 16 bits
#define         BPF_B           0x10 // Ld/Ldx fields. Data size - byte - 8 bits
#define BPF_MODE(code)  ((code) & 0xe0)
#define         BPF_IMM         0x00 // Ld/Ldx fields. Addressing mode - /constant/
#define         BPF_ABS         0x20 // Ld/Ldx fields. Addressing mode - /packet data at a fixed offset/
#define         BPF_IND         0x40 // Ld/Ldx fields. Addressing mode - /packet data at a variable offset/
#define         BPF_MEM         0x60 // Ld/Ldx fields. Addressing mode - /load the packet length/
#define         BPF_LEN         0x80 // Ld/Ldx fields. Addressing mode - /load word (32 bits) from the scratch memory store/
#define         BPF_MSH         0xa0 // Ld/Ldx fields. Addressing mode - /efficiently loading the IP header length/

//---------------------------------------------------------------------------
// alu/jmp fields 
#define BPF_OP(code)    ((code) & 0xf0)
#define         BPF_ADD         0x00 // Alu operation. A <- A + [k,X]
#define         BPF_SUB         0x10 // Alu operation. A <- A - [k,X]
#define         BPF_MUL         0x20 // Alu operation. A <- A * [k,X]
#define         BPF_DIV         0x30 // Alu operation. A <- A / [k,X]
#define         BPF_OR          0x40 // Alu operation. A <- A or [k,X]
#define         BPF_AND         0x50 // Alu operation. A <- A and [k,X]
#define         BPF_LSH         0x60 // Alu operation. A <- A << [k,X]
#define         BPF_RSH         0x70 // Alu operation. A <- A >> [k,X]
#define         BPF_NEG         0x80 // Alu operation. A <- (-A)

#define         BPF_JA          0x00 // Jmp operation. pc += k
#define         BPF_JEQ         0x10 // Jmp operation. pc += (A == [k,X]) ? jt : jf
#define         BPF_JGT         0x20 // Jmp operation. pc += (A > [k,X]) ? jt : jf
#define         BPF_JGE         0x30 // Jmp operation. pc += (A >= [k,X]) ? jt : jf
#define         BPF_JSET        0x40 // Jmp operation. pc += (A and [k,X]) ? jt : jf

#define BPF_SRC(code)   ((code) & 0x08)
#define         BPF_K           0x00 // Alu/Jmp fields. Source mode - /index register/.
#define         BPF_X           0x08 // Alu/Jmp/Ret fields. Source mode - /k/.

//---------------------------------------------------------------------------
// ret - BPF_K and BPF_X also apply 
#define BPF_RVAL(code)  ((code) & 0x18)
#define         BPF_A           0x10 // Ret fields. Source mode - /accumulator/.

//---------------------------------------------------------------------------
// misc 
#define BPF_MISCOP(code) ((code) & 0xf8)
#define         BPF_TAX         0x00 // Misc operation. X <- A
#define         BPF_TXA         0x80 // Misc operation. A <- X

#define         BPF_HTONL       0x10 // Misc operation. A <- htonl(A)
#define         BPF_HTONS       0x20 // Misc operation. A <- htons(A)


//---------------------------------------------------------------------------
// Macros for insn array initializers.
//---------------------------------------------------------------------------
#define BPF_STMT(code, k)         { (USHORT)(code),  0,  0, k }
#define BPF_JUMP(code, k, jt, jf) { (USHORT)(code), jt, jf, k }

//---------------------------------------------------------------------------
// Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
//---------------------------------------------------------------------------
#define BPF_MEMWORDS   16

//---------------------------------------------------------------------------
// Max programm instructions count
//---------------------------------------------------------------------------
#define BPF_MAXINSNS   4096

//---------------------------------------------------------------------------
#endif // __HN_BPF_H__
//---------------------------------------------------------------------------
